// import path from 'path'
import i18n from '@/i18n'

export const generateRoutes = (routes) => {
  const res = []
  generateRoute(res, routes)
  return res
}
/**
 * 筛选出可供搜索的路由对象
 * @param routes 路由表
 * @param isChildren
 */

const generateRoute = (res, routes, isChildren = false) => {
  for (let index = 0; index < routes.length; index++) {
    const data = {
      path: routes[index].path,
      title: isChildren ? convertPathToCamelCase(routes[index].path) : [i18n.global.t(`msg.route.${routes[index].meta.title}`)]
    }
    res.push(data)
    if (routes[index].children.length !== 0) {
      generateRoute(res, [...routes[index].children], true)
    }
  }
  // return res
}

function convertPathToCamelCase(path) {
  // ['', 'user', 'manage']
  const parts = path.split('/')
  const words = parts.slice(1)
  const camelCaseWords = words.map((word, index) => {
    // eslint-disable-next-line array-callback-return
    if (index === 0) return word
    return word.charAt(0).toUpperCase() + word.slice(1)
  })
  // return [parts[1], camelCaseWords.join('')]
  return [i18n.global.t(`msg.route.${parts[1]}`), i18n.global.t(`msg.route.${camelCaseWords.join('')}`)]
}
